package com.sanpu.kxe.business.main.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.MyWxInfoContract;
import com.sanpu.kxe.business.main.presenter.MyWxInfoPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;
import com.sanpu.library.imagechoose.PhotoPicker;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：我的微信信息
 * Created by 薛勇军 on 2018/7/10.
 */

public class MyWxInfoActivity extends BaseTitleActivity implements MyWxInfoContract.View {

    @Bind(R.id.name)
    EditTextWithDel vName;
    @Bind(R.id.qrcodeIv)
    ImageView vQrcodeIv;

    private MyWxInfoContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_wx_info;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的微信");
        commonTitle.showRightTextView("保存", ContextCompat.getColor(mContext, R.color.color_base));

        mPresenter = new MyWxInfoPresenter(this);
        mPresenter.userWxInfo();
    }

    @OnClick({R.id.updateRtv, R.id.qrcodeLayout})
    public void onClick(View view) {
        if (view.getId() == R.id.updateRtv) {
            mPresenter.picker(CommonConfig.REQUESTCODE_PICKER_QRCODE);
        } else if (view.getId() == R.id.qrcodeLayout) {
            if (vQrcodeIv.getTag() != null) {
                mPresenter.preview((String) vQrcodeIv.getTag(), CommonConfig.REQUESTCODE_VIEW_QRCODE);
            }
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        vName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String editable = vName.getText().toString();
                String str = VerifyUtil.stringFilter(editable);
                if(!editable.equals(str)){
                    vName.setText(str);
                    //设置新的光标所在位置
                    vName.setSelection(str.length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String wxChatId = vName.getText().toString();
                if (TextUtils.isEmpty(wxChatId)) {
                    ToastUtil.showTextViewPrompt("请输入微信号");
                    return;
                }
                if (vQrcodeIv.getTag() == null) {
                    ToastUtil.showTextViewPrompt("请选择微信二维码照片");
                    return;
                }
                mPresenter.onCommit(wxChatId);
            }
        });
    }

    @Override
    public void showWxChatId(String wxChatId) {
        if (!TextUtils.isEmpty(wxChatId)) {
            vName.setText(wxChatId);
        }
    }

    @Override
    public void showWxQrcodeImg(String wxQrcodeImg) {
        if (!TextUtils.isEmpty(wxQrcodeImg)) {
            String imgUrl = UrlConstants.getBaseUrl() + wxQrcodeImg + RequestMap.sessionIdReqStr();
            JKImageLoader.getInstance().displayImage(mContext, imgUrl, vQrcodeIv);
            vQrcodeIv.setTag(imgUrl);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == Activity.RESULT_OK) {
            ArrayList<String> photos =
                    data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            String fileIdName = MainConfig.TYPE_WXQRCODEIMG;
            if (requestCode == CommonConfig.REQUESTCODE_VIEW_QRCODE) {
                if (photos == null || photos.size() == 0) {
                    vQrcodeIv.setTag(null);
                    vQrcodeIv.setImageDrawable(null);
                    mPresenter.removeFile(fileIdName);
                }
                return;
            }
            String filePath = (photos != null && photos.size()>0)? photos.get(0) : null;
            if (TextUtils.isEmpty(filePath)) {
                return;
            }
            if (requestCode == CommonConfig.REQUESTCODE_PICKER_QRCODE) {
                JKImageLoader.getInstance().displayImage(mContext, filePath, vQrcodeIv);
                vQrcodeIv.setTag(filePath);
            }

            mPresenter.onCompress(photos.get(0), fileIdName);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MyWxInfoContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
